package com.lsz.deport.ui.order;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.SimpleItemAnimator;

import com.common.mvp.BaseMvpActivity;
import com.common.widght.IncludeTitleLayout;
import com.lsz.deport.R;
import com.lsz.deport.event.StartOrderEvent;
import com.lsz.deport.json.OrderDetailBean;
import com.util_code.dialog.LoadingDialog;
import com.util_code.statusbar.StatusTopBarUtil;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.List;
import java.util.Objects;

import butterknife.BindView;

public class OrderListActivity extends BaseMvpActivity<OrderView, OrderPresent> implements OrderView {

    @BindView(R.id.itl_toolbar)
    IncludeTitleLayout itlToolbar;
    @BindView(R.id.rv_detail_list)
    RecyclerView rvDetailList;

    private OrderAdapter orderAdapter;
    private LoadingDialog loadingDialog;
    private String orderId = "0";

    public static void startActivity(Activity activity, String id) {
        Intent intent = new Intent(activity, OrderListActivity.class);
        intent.putExtra("OrderId", id);
        activity.startActivity(intent);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_order_list;
    }

    @Override
    protected void initInject() {
        getActivityComponent().inject(this);
    }

    @Override
    protected void setTranslateStatusBar(boolean barColor) {
        super.setTranslateStatusBar(false);
    }

    @Override
    protected void setupView() {
        itlToolbar.setTitleText("订单商品", R.color.app_white);
        itlToolbar.setTitleBarHeight(View.VISIBLE);
        itlToolbar.setLeftImage(R.drawable.icon_left_white);
        itlToolbar.setToolbarClickListener(toolbarClickListener);

        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        rvDetailList.setLayoutManager(linearLayoutManager);
        ((SimpleItemAnimator) Objects.requireNonNull(rvDetailList.getItemAnimator())).setSupportsChangeAnimations(false);
        orderAdapter = new OrderAdapter(this);
        rvDetailList.setAdapter(orderAdapter);

        if (loadingDialog == null) {
            loadingDialog = new LoadingDialog();
        }
    }

    @Override
    protected void setupData(Bundle savedInstanceState) {
        Intent intent = getIntent();
        orderId = intent.getStringExtra("OrderId");

        getPresenter().getOrderDetail(orderId);
    }

    private IncludeTitleLayout.ToolbarClickListener toolbarClickListener = new IncludeTitleLayout.ToolbarClickListener() {
        @Override
        public void onLeftClick() {
            finish();
        }

        @Override
        public void onRightClick() {

        }
    };


    public void onClick(View view) {

    }


    @Override
    public <T> void onSuccess(T t) {
        if (loadingDialog.isVisible()) {
            loadingDialog.dismissAllowingStateLoss();
        }
        List<OrderDetailBean> orderDetailList = (List<OrderDetailBean>) t;
        orderAdapter.resetData(orderDetailList);
    }

    @Override
    public void onFail() {
        if (loadingDialog.isVisible()) {
            loadingDialog.dismissAllowingStateLoss();
        }
    }
}
